//
//  AutoSize+PPKit.swift
//  PPKit
//
//  Created by xiaopin on 2019/7/23.
//  Copyright © 2019 PPKit. All rights reserved.
//  https://gitee.com/xiaopindev/PPKit
//  https://github.com/xiaopindev
//  https://xiaopin.cnblogs.com

import UIKit

//MARK: 标签、文本动态计算宽高的方法
public extension String{
    
    /// 根据 #字符串# 计算宽度尺寸
    ///
    /// - Parameters:
    ///   - font: <#font description#>
    ///   - height: <#height description#>
    /// - Returns: <#return value description#>
    func autoWidth(font: UIFont, height: CGFloat = 20) -> CGFloat {
        let attributes: [NSAttributedString.Key: Any] = [.font : font]
        let size = self.boundingRect(with: CGSize(width: .greatestFiniteMagnitude, height: height),
                                     options: NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin),
                                     attributes: attributes,
                                     context: nil).size
        return size.width
    }
    
    /// 根据 #字符串# 计算高度尺寸
    ///
    /// - Parameters:
    ///   - font: <#font description#>
    ///   - width: <#width description#>
    /// - Returns: <#return value description#>
    func autoHeight(font: UIFont, width: CGFloat = 20) -> CGFloat {
        let attributes: [NSAttributedString.Key: Any] = [.font : font]
        let size = self.boundingRect(with: CGSize(width: width, height: .greatestFiniteMagnitude),
                                     options: NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin),
                                     attributes: attributes,
                                     context: nil).size
        return size.height
    }
}

public extension NSMutableAttributedString{
    
    /// 根据 #属性字符串# 计算宽度尺寸
    ///
    /// - Parameters:
    ///   - attrs: <#attrs description#>
    ///   - height: <#height description#>
    /// - Returns: <#return value description#>
    func autoWidth(h: CGFloat = 20) -> CGFloat {
        let size = self.boundingRect(with: CGSize(width: .greatestFiniteMagnitude, height:h), options: [.usesLineFragmentOrigin, .usesFontLeading], context: nil)
        return size.width
    }
    
    /// 根据 #属性字符串# 计算高度尺寸
    ///
    /// - Parameters:
    ///   - attrs: <#attrs description#>
    ///   - width: <#width description#>
    /// - Returns: <#return value description#>
    func autoHeight(w: CGFloat = 20) -> CGFloat {
        let size = self.boundingRect(with: CGSize(width: w, height: .greatestFiniteMagnitude), options: [.usesLineFragmentOrigin, .usesFontLeading], context: nil)
        return size.height
    }

    /// 获取指定宽度的自适应大小
    ///
    /// - Parameter w: <#w description#>
    /// - Returns: <#return value description#>
    func sizeToFit(w:CGFloat) -> CGSize{
        let label = UILabel(frame: CGRect(x: 0, y: 0, width: w, height: 0))
        label.attributedText = self
        label.numberOfLines = 0
        label.lineBreakMode = .byWordWrapping
        label.sizeToFit()
        return label.frame.size
    }
}

public extension UILabel{
    
    /// 根据 #标签# 获取自适应宽度
    ///
    /// - Returns: <#return value description#>
    func autoWidth() -> CGFloat {
        guard let text = self.text else{return 0}
        let attributes: [NSAttributedString.Key: Any] = [.font : self.font!]
        let size:CGSize = text.boundingRect(with: CGSize(width: .greatestFiniteMagnitude, height: self.bounds.size.height),
                                                  options: NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin),
                                                  attributes: attributes,
                                                  context: nil).size
        return ceil(size.width)
    }
    
    /// 根据 #标签# 获取自适应高度
    ///
    /// - Returns: <#return value description#>
    func autoHeight() -> CGFloat {
        guard let text = self.text else{return 0}
        let attributes: [NSAttributedString.Key: Any] = [.font : self.font!]
        let size:CGSize = text.boundingRect(with: CGSize(width: self.bounds.size.width, height: .greatestFiniteMagnitude),
                                                  options: NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin),
                                                  attributes: attributes,
                                                  context: nil).size
        return ceil(size.height)
    }
}
